package com.work.formaldehyde.activity;

import android.Manifest;
import android.app.ActivityOptions;
import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.google.android.material.tabs.TabLayout;
import com.google.gson.Gson;
import com.lljjcoder.Interface.OnCityItemClickListener;
import com.lljjcoder.bean.CityBean;
import com.lljjcoder.bean.DistrictBean;
import com.lljjcoder.bean.ProvinceBean;
import com.lljjcoder.citywheel.CityConfig;
import com.lljjcoder.style.citypickerview.CityPickerView;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.umeng.analytics.MobclickAgent;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.work.formaldehyde.R;
import com.work.formaldehyde.activityadapter.DiZhiAdapter;
import com.work.formaldehyde.activityadapter.PopupGirdAdapter;
import com.work.formaldehyde.model.GetAddressModel;
import com.work.formaldehyde.model.ShangPingXiangQingModel;
import com.work.formaldehyde.util.ApiUtils;
import com.work.formaldehyde.util.Url;
import com.work.formaldehyde.view.RoundImageView;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoader;

import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import static com.work.formaldehyde.util.Url.FENXIANG_IMG;

public class ShangPingActivity extends AppCompatActivity implements OnClickListener, OnBannerListener {

    private ImageView jiance_comeback, jiance_comebacks, home_title_remind, home_title_reminds, home_title_share, home_title_shares;
    private LinearLayout check_gengduo, fwtk_line;
    private RelativeLayout addgwc, nowyuyue, dibu, xqtitle,yonghupl;
    private static PopupWindow popupWindow;
    private static PopupWindow popupWindows;
    private LinearLayout addrela, xiangqiline, xuanze_line, peisong_line, to_gouwuche;
    private LinearLayout pinglunzhanshi, shangpingxiangqings;
    private ScrollView shangping_scro;
    private TabLayout mytab;
    private RelativeLayout xqtitles;
    private RoundImageView user_headimgs;
    private TextView homeitem_text, thismoney, pinglunnum, usermsg, yunfei_text, yuanjias, peisong_text, username;
    public static boolean ischeckaddress = false;
    private CityPickerView mPicker = new CityPickerView();

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ApiUtils.ActivityIsLogin(this, 1);
        setContentView(R.layout.activity_spen);
        jiance_comebacks = findViewById(R.id.jiance_comebacks);
        jiance_comebacks.setOnClickListener(this);
        jiance_comeback = findViewById(R.id.jiance_comeback);
        jiance_comeback.setOnClickListener(this);
        if (Build.VERSION.SDK_INT >= 23) {
            String[] mPermissionList = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.READ_PHONE_STATE, Manifest.permission.READ_EXTERNAL_STORAGE};
            ActivityCompat.requestPermissions(this, mPermissionList, 123);
        }
        if (ApiUtils.isNetworkConnected(ShangPingActivity.this))
            inti();
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }
    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    private int getValue = 0;

    private Banner mBanner;
    private ArrayList<String> imagePath = new ArrayList<>();
    private ArrayList<String> imageTitle;
    private ArrayList<String> msgtext;
    private IWXAPI api;
    private MyImageLoader mMyImageLoader;

    /**
     * 用于遍历集合
     */
    public void ListInit() {
        // 通过WXAPIFactory工厂，获取IWXAPI的实例
        api = WXAPIFactory.createWXAPI(this, Url.WXAPPID, true);
        // 将应用的appId注册到微信
        api.registerApp(Url.WXAPPID);
        imageTitle = new ArrayList<>();
        msgtext = new ArrayList<>();
        imageTitle.add("我是海鸟一号");
        imageTitle.add("我是海鸟二号");
        imageTitle.add("我是海鸟三号");
        msgtext.add("恭喜【简单爱】完成每日任务，获得0.10元奖");
        msgtext.add("恭喜【简单爱】完成每日任务，获得0.20元奖");
        msgtext.add("恭喜【简单爱】完成每日任务，获得0.30元奖");
    }

    /**
     * 图片加载类
     */
    private class MyImageLoader extends ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            Glide.with(context.getApplicationContext())
                    .load(path)
                    .into(imageView);
        }
    }

    private void initViews() {
        mMyImageLoader = new MyImageLoader();
        //设置样式，里面有很多种样式可以自己都看看效果
        mBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        //设置图片加载器
        mBanner.setImageLoader(mMyImageLoader);
        //设置轮播的动画效果,里面有很多种特效,可以都看看效果。
        mBanner.setBannerAnimation(Transformer.DepthPage);
        //轮播图片的文字
        mBanner.setBannerTitles(imageTitle);
        //设置轮播间隔时间
        mBanner.setDelayTime(99999999);
        //设置是否为自动轮播，默认是true
        mBanner.isAutoPlay(true);
        //设置指示器的位置，小点点，居中显示
        mBanner.setIndicatorGravity(BannerConfig.CENTER);
        //设置图片加载地址
        mBanner.setImages(imagePath)
                //轮播图的监听
                .setOnBannerListener(this)
                //开始调用的方法，启动轮播图。
                .start();

    }


    /**
     * 初始化加载调用方法
     */
    @RequiresApi(api = Build.VERSION_CODES.M)
    public void inti() {
        mPicker.init(this);
        Intent getint = getIntent();
        goodsid = getint.getStringExtra("id");
        manjian = getint.getStringExtra("manjian");
        user_headimgs = findViewById(R.id.user_headimgs);
        shangpingxiangqings = findViewById(R.id.shangpingxiangqings);
        home_title_remind = findViewById(R.id.home_title_remind);
        home_title_reminds = findViewById(R.id.home_title_reminds);
        home_title_share = findViewById(R.id.home_title_share);
        yonghupl=findViewById(R.id.yonghupl);
        home_title_shares = findViewById(R.id.home_title_shares);
        fwtk_line = findViewById(R.id.fwtk_line);
        fwtk_line.setOnClickListener(this);
        yunfei_text = findViewById(R.id.yunfei_text);
        username = findViewById(R.id.username);
        home_title_remind.setOnClickListener(this);
        home_title_reminds.setOnClickListener(this);
        home_title_share.setOnClickListener(this);
        home_title_shares.setOnClickListener(this);
        thismoney = findViewById(R.id.thismoney);
        pinglunzhanshi = findViewById(R.id.pinglunzhanshi);
        pinglunnum = findViewById(R.id.pinglunnum);
        peisong_text = findViewById(R.id.peisong_text);
        yuanjias = findViewById(R.id.yuanjia);
        xqtitles = findViewById(R.id.xqtitles);
        xqtitle = findViewById(R.id.xqtitle);
        usermsg = findViewById(R.id.usermsg);
        mBanner = findViewById(R.id.spen_banner);
        manjian_msg = findViewById(R.id.manjian_msg);
        manjian_msg.setText(manjian);
        homeitem_text = findViewById(R.id.homeitem_text);
        shangping_scro = findViewById(R.id.shangping_scro);
        check_gengduo = findViewById(R.id.check_gengduo);
        check_gengduo.setOnClickListener(this);
        peisong_line = findViewById(R.id.peisong_line);
        peisong_line.setOnClickListener(this);
        xuanze_line = findViewById(R.id.xuanze_line);
        xuanze_line.setOnClickListener(this);
        dibu = findViewById(R.id.dibu);
        addgwc = findViewById(R.id.addgwc);
        addgwc.setOnClickListener(this);
        nowyuyue = findViewById(R.id.nowyuyue);
        nowyuyue.setOnClickListener(this);
        addrela = findViewById(R.id.addrela);
        to_gouwuche = findViewById(R.id.to_gouwuche);
        to_gouwuche.setOnClickListener(this);
        xiangqiline = findViewById(R.id.xiangqiline);
//        for (int i = 0; i < 3; i++) {
//            RelativeLayout relativeLayout = new RelativeLayout(ShangPingActivity.this);
//            TextView textView = new TextView(ShangPingActivity.this);
//            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
//            lp.setMargins(24, 24, 0, 24);
//            LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
//            lp1.setMargins(24, 6, 24, 6);
//            relativeLayout.setLayoutParams(lp);
//            relativeLayout.setBackgroundResource(R.drawable.dazhe_text_rela);
//            textView.setLayoutParams(lp1);
//            textView.setText("满100减" + i);
//            textView.setTextColor(getResources().getColor(R.color.home_bottom_text_color_ischeck));
//            textView.setTextSize(14);
//            addrela.addView(relativeLayout);
//            relativeLayout.addView(textView);
//            Init();
//        }
        peisong_text.setText(Url.ADDRESSCITY);
        mytab = findViewById(R.id.head_tab);
        mytab.addTab(mytab.newTab().setText("商品"));
        mytab.addTab(mytab.newTab().setText("评价"));
        mytab.addTab(mytab.newTab().setText("详情"));
        mytab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (tab.getText().equals("商品")) {
                    shangping_scro.fullScroll(ScrollView.FOCUS_UP);
                }
                if (tab.getText().equals("评价")) {
                    shangping_scro.scrollTo(0,  yonghupl.getTop());
                }
                if (tab.getText().equals("详情")) {
                    shangping_scro.scrollTo(0,  shangpingxiangqings.getTop());
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
        shangping_scro.setOnScrollChangeListener(new View.OnScrollChangeListener() {
            @Override
            public void onScrollChange(View view, int i, int i1, int i2, int i3) {
                if (isVisibleLocal(mBanner, false)) {
                    xqtitles.setVisibility(View.GONE);
                    xqtitle.setVisibility(View.VISIBLE);
                } else {
                    xqtitles.setVisibility(View.VISIBLE);
                    xqtitle.setVisibility(View.GONE);
                }
            }
        });
        if (ApiUtils.isNetworkConnected(ShangPingActivity.this))
            okHttp_postFromParameters();
        if (ApiUtils.isNetworkConnected(ShangPingActivity.this) && !Url.USER_ID.equals(""))
            okHttp_postFromParameters1();
        Map<String, Object> music = new HashMap<String, Object>();
        music.put("详情", "1");//自定义参数：音乐类型，值：流行
        MobclickAgent.onEventObject(ShangPingActivity.this, "details", music);
    }

    /**
     * 判断View是否可见
     *
     * @param target   View
     * @param judgeAll 为true时,判断View全部可见才返回true
     * @return boolean
     */
    public static boolean isVisibleLocal(View target, boolean judgeAll) {
        Rect rect = new Rect();
        target.getLocalVisibleRect(rect);
        if (judgeAll) {
            return rect.top == 0;
        } else {
            return rect.top >= 0;
        }
    }

    public static ArrayList<String> new_list_views;
    public static ArrayList<String> new_list_view;
    private PopupGirdAdapter myAdapters;
    private DiZhiAdapter myAdapter;

    public void Init() {
        new_list_views = new ArrayList<>();
        new_list_view = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            new_list_views.add("" + i);
        }
        myAdapters = new PopupGirdAdapter(new_list_views,
                ShangPingActivity.this, true);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fwtk_line:
                Intent fwtk = new Intent(ShangPingActivity.this, FuWuTiaoKuanActivity.class);
                startActivity(fwtk, ActivityOptions.makeSceneTransitionAnimation(ShangPingActivity.this).toBundle());
                break;
            case R.id.jiance_comeback:
                onBackPressed();
                break;
            case R.id.home_title_remind:
                Intent tz1 = new Intent(ShangPingActivity.this, MsgListActivity.class);
                startActivity(tz1, ActivityOptions.makeSceneTransitionAnimation(ShangPingActivity.this).toBundle());
                break;
            case R.id.home_title_reminds:
                Intent tz = new Intent(ShangPingActivity.this, MsgListActivity.class);
                startActivity(tz , ActivityOptions.makeSceneTransitionAnimation(ShangPingActivity.this).toBundle());
                break;
            case R.id.home_title_share:
                fenxiang();
                break;
            case R.id.home_title_shares:
                fenxiang();
                break;
            case R.id.jiance_comebacks:
                onBackPressed();
                break;
            case R.id.addgwc:
                if (Url.USER_ID.equals("")) {
                    Intent is_login = new Intent(ShangPingActivity.this, LoginActivity.class);
                    startActivity(is_login, ActivityOptions.makeSceneTransitionAnimation(ShangPingActivity.this).toBundle());
                    ApiUtils.SetToast(ShangPingActivity.this, "请先登录");
                } else {
                    sjcs = 1;
                    if (ApiUtils.isNetworkConnected(ShangPingActivity.this)) {
                        ApiUtils.windows(ShangPingActivity.this, addgwc, "请稍后");
                        new Handler().postDelayed(new Runnable() {
                            public void run() {
                                ApiUtils.closepopup();
                            }
                        }, 10000);
                        okHttp_postFromParameters();
                    }
                }
                break;
            case R.id.peisong_line:
                windows(ShangPingActivity.this);
                break;
            case R.id.nowyuyue:
                if (Url.USER_ID.equals("")) {
                    Intent is_login = new Intent(ShangPingActivity.this, LoginActivity.class);
                    startActivity(is_login, ActivityOptions.makeSceneTransitionAnimation(ShangPingActivity.this).toBundle());
                    ApiUtils.SetToast(ShangPingActivity.this, "请先登录");
                } else {
                    if (goodsid.equals("") || jiage.equals("")) {
                        ApiUtils.SetToast(ShangPingActivity.this, "获取商品信息中..");
                    } else {
                        Intent qrdd = new Intent(ShangPingActivity.this, QueRenDingDanActivity.class);
                        qrdd.putExtra("shangpingid", "" + goodsid);
                        qrdd.putExtra("shangpingmoney", "" + jiage);
                        qrdd.putExtra("shangpingimg", "" +shangPingModel.getData().getGoods_img());
                        qrdd.putExtra("dingdan", "false");
                        qrdd.putExtra("allmoney", "");
                        if (suantianshu.equals("0"))
                            qrdd.putExtra("suantianshu", "0");
                        else
                            qrdd.putExtra("suantianshu", "1");
                        startActivity(qrdd, ActivityOptions.makeSceneTransitionAnimation(ShangPingActivity.this).toBundle());
                    }
                }
                break;
            case R.id.xuanze_line:
                window(ShangPingActivity.this);
                break;
            case R.id.check_gengduo:
                if (comment.size() > 0) {
                    Intent ckgd = new Intent(ShangPingActivity.this, CommentActivity.class);
                    startActivity(ckgd, ActivityOptions.makeSceneTransitionAnimation(ShangPingActivity.this).toBundle());
                } else {
                    ApiUtils.SetToast(ShangPingActivity.this, "暂无评论哦");
                }

                break;
            case R.id.to_gouwuche:
                if (Url.USER_ID.equals("")) {
                    Intent is_login = new Intent(ShangPingActivity.this, LoginActivity.class);
                    startActivity(is_login, ActivityOptions.makeSceneTransitionAnimation(ShangPingActivity.this).toBundle());
                    ApiUtils.SetToast(ShangPingActivity.this, "请先登录");
                } else {
                    Intent togwc = new Intent(ShangPingActivity.this, GouWuCheActivity.class);
                    startActivity(togwc, ActivityOptions.makeSceneTransitionAnimation(ShangPingActivity.this).toBundle());
                }
                break;
        }
    }

    private CheckBox isTimelineCb;
    private static final int THUMB_SIZE = 150;

    public void fenxiang() {
        UMImage image = new UMImage(ShangPingActivity.this, Url.FENXIANG_IMG);
        UMWeb web = new UMWeb("https://sj.qq.com/myapp/detail.htm?apkName=com.work.formaldehyde");
        web.setTitle(Url.FENXIANG_TITLE);//标题
        web.setThumb(image);  //缩略图
        web.setDescription("优质生活，从甲醛测试开始");//描述
        new ShareAction(ShangPingActivity.this).setPlatform(SHARE_MEDIA.WEIXIN).withText(Url.FENXIANG_TITLE)
                .withMedia(web)
                .share();
    }

    private String buildTransaction(final String type) {
        return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
    }


    int guigenum = 1;
    private TextView zhekoujia, yuanjia, addtext, manjian_msg;

    public void window(Context context) {
        View popupwindowview = LayoutInflater.from(context).inflate(
                R.layout.popupwindow_guige, null);
        GridView guige_grid = popupwindowview.findViewById(R.id.guige_grid);
        RelativeLayout addgwc = popupwindowview.findViewById(R.id.addgwc);
        zhekoujia = popupwindowview.findViewById(R.id.zhekoujia);
        yuanjia = popupwindowview.findViewById(R.id.yuanjia);
        RelativeLayout dazhe = popupwindowview.findViewById(R.id.dazhe);
        RelativeLayout jian = popupwindowview.findViewById(R.id.jian);
        RelativeLayout add = popupwindowview.findViewById(R.id.add);
        addtext = popupwindowview.findViewById(R.id.addtext);
        addtext.setText(1 + "");
        addgwc.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
        jian.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (guigenum > 1) {
                    guigenum--;
                    addtext.setText(guigenum + "");
                }
            }
        });
        add.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                guigenum++;
                addtext.setText(guigenum + "");
            }
        });
        guige_grid.setAdapter(myAdapters);
        guige_grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//                myAdapters.notifyDataSetChanged();
            }
        });
        popupWindow = new PopupWindow(popupwindowview, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setAnimationStyle(R.style.popwin_anim_style);
        popupWindow.setTouchable(true);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                // TODO Auto-generated method stub
                WindowManager.LayoutParams lp = getWindow().getAttributes();
                lp.alpha = 1f;
                getWindow().setAttributes(lp);
            }
        });
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = 0.4f;
        getWindow().setAttributes(lp);
        popupWindow.showAtLocation(dibu, Gravity.BOTTOM, 0, 0);
    }

    public static String addname = "", addphone = "", addresstrue = "";

    public void windows(Context context) {
        View popupwindowviews = LayoutInflater.from(context).inflate(
                R.layout.popupwindow_dizhi, null);
        ListView guige_grid = popupwindowviews.findViewById(R.id.dizhi_check_list);
        RelativeLayout qtps = popupwindowviews.findViewById(R.id.qtps);
        guige_grid.setAdapter(myAdapter);
        guige_grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                for (int is = 0; is < new_list_view.size(); is++) {
                    if (new_list_view.get(is).equals("2") && is != i) {
                        new_list_view.set(is, "1");
                    }
                }
                new_list_view.set(i, "2");
                ischeckaddress = true;
                addname = list_view.get(i).getConsignee_name();
                addphone = list_view.get(i).getConsignee_phone();
                addresstrue = list_view.get(i).getConsignee_addr();
                myAdapter.notifyDataSetChanged();
            }
        });
        qtps.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindows.dismiss();
                opencity();
            }
        });
        popupWindows = new PopupWindow(popupwindowviews, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT, true);
        popupWindows.setAnimationStyle(R.style.popwin_anim_style);
        popupWindows.setTouchable(true);
        popupWindows.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                // TODO Auto-generated method stub
                WindowManager.LayoutParams lp = getWindow().getAttributes();
                lp.alpha = 1f;
                getWindow().setAttributes(lp);
            }
        });
        popupWindows.setFocusable(true);
        popupWindows.setOutsideTouchable(true);
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = 0.4f;
        getWindow().setAttributes(lp);
        popupWindows.showAtLocation(dibu, Gravity.BOTTOM, 0, 0);
    }

    public void opencity() {
        //添加默认的配置，不需要自己定义，当然也可以自定义相关熟悉，详细属性请看demo
        CityConfig cityConfig = new CityConfig.Builder().title("选择其他城市").setShowGAT(true).build();

        mPicker.setConfig(cityConfig);
        mPicker.setOnCityItemClickListener(new OnCityItemClickListener() {
            @Override
            public void onSelected(ProvinceBean province, CityBean city, DistrictBean district) {
                peisong_text.setText(province.toString() + city.toString() + district.toString());
            }

            @Override
            public void onCancel() {
            }
        });
        mPicker.showCityPicker();
    }

    private int sjcs = 0;

    private String goodsid, manjian;

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ApiUtils.popupWindows != null && ApiUtils.popupWindows.isShowing()) {
            return false;
        }
        return super.dispatchTouchEvent(ev);
    }

    private void okHttp_postFromParameters() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient okHttpClient = new OkHttpClient();
                    if (sjcs == 1) {
                        RequestBody formBody = new FormBody.Builder().add("uid", Url.USER_ID).add("logintoken", Url.LOGIN_TOKEN).add("num", "1").add("goodsid", goodsid)
                                .build();
                        Request request = new Request.Builder().url(Url.GOUWUCHEUP).post(formBody).build();
                        okhttp3.Response response = okHttpClient.newCall(request).execute();
                        String msg = response.body().string();
                        Message message = new Message();
                        message.obj = msg;
                        gethot1.sendMessage(message);
                    } else {
//                        RequestBody formBody = new FormBody.Builder().add("goodsid", goodsid)
//                                .build();
//                        Request request = new Request.Builder().url(Url.GETSHANGPINGXIANGQING).post(formBody).build();
//                        okhttp3.Response response = okHttpClient.newCall(request).execute();
//                        String msg = response.body().string();
//                        Message message = new Message();
//                        message.obj = msg;
//                        gethot2.sendMessage(message);
                        Request request = new Request.Builder().get().url(Url.SHANGPINGXIANGQING+goodsid).build();
                        Call call = okHttpClient.newCall(request);
                        call.enqueue(new Callback() {
                            @Override
                            public void onFailure(Call call, IOException e) {
                            }

                            @Override
                            public void onResponse(Call call, final Response response) {
                                try {
                                    final String responseStr;
                                    responseStr = response.body().string();
                                    Message message = new Message();
                                    message.obj = responseStr;
                                    gethot2.sendMessage(message);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        });
                    }
                } catch (
                        Exception e) {
                    e.printStackTrace();
                }
            }
        }).
                start();
    }

    private String suantianshu = "0";

    public Handler gethot1 = new Handler() {
        public void handleMessage(android.os.Message msg) {
            JSONObject success_Object;
            try {
                if (msg.obj != null) {
                    ApiUtils.closepopup();
                    success_Object = new JSONObject(msg.obj.toString());
                    String code = success_Object.getString("code");
                    ApiUtils.SetToast(ShangPingActivity.this, success_Object.getString("msg"));
                    sjcs = 0;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    private String jiage = "";
    public static ArrayList<ShangPingXiangQingModel.comment> comment = new ArrayList<>();
    private ArrayList<ShangPingXiangQingModel.info> comments = new ArrayList<>();
    private ShangPingXiangQingModel shangPingModel;
    public Handler gethot2 = new Handler() {
        public void handleMessage(android.os.Message msg) {
            JSONObject success_Object;
            try {
                if (msg.obj != null) {
                    success_Object = new JSONObject(msg.obj.toString());
                    String code = success_Object.getString("code");
                    if (code.equals("1")) {
                        Gson gson = new Gson();
                        shangPingModel = gson.fromJson(msg.obj.toString(),
                                ShangPingXiangQingModel.class);
                        comment.clear();
                        comment.addAll(shangPingModel.getData().comment);
                        if (comment.size() == 0)
                            pinglunzhanshi.setVisibility(View.GONE);
                        suantianshu=shangPingModel.getData().getIs_day();
                        homeitem_text.setText(shangPingModel.getData().getGoods_name());
                        ListInit();
                        thismoney.setText(shangPingModel.getData().getGoods_discount_price());
                        jiage = shangPingModel.getData().getGoods_discount_price();
                        if (comment.size() != 0) {
                            pinglunnum.setText("用户评价(" + comment.size() + ")");
                            Glide.with(ShangPingActivity.this)
                                    .load(comment.get(0).getUser_head())
                                    .into(user_headimgs);
                            username.setText(comment.get(0).getUser_name());
                            usermsg.setText(comment.get(0).getContent());
                        } else
                            pinglunnum.setText("用户评价(0)");
                        yuanjias.setText("¥" + shangPingModel.getData().getGoods_price());
                        yuanjias.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                        yunfei_text.setText(shangPingModel.getData().getGoods_freight());

                        comments.addAll(shangPingModel.getData().info);
                        if (shangPingModel.getData().getInfo().size() > 0)
                            shangpingxiangqings.setVisibility(View.GONE);
                        for (int i = 0; i < shangPingModel.getData().getInfo().size(); i++) {
                            final ImageView imageView = new ImageView(ShangPingActivity.this);
                            Glide.with(ShangPingActivity.this)
                                    .load(comments.get(i).getImg())
                                    .skipMemoryCache(false)
                                    .into(new SimpleTarget<Drawable>() {
                                        @Override
                                        public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                                            int width = resource.getIntrinsicWidth();//图片原始宽度
                                            int height = resource.getMinimumHeight();//图片原始高度
                                            DisplayMetrics outMetrics = new DisplayMetrics();
                                            getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
                                            int widthPixels = outMetrics.widthPixels;
                                            int scaledW = (height * widthPixels) / width;
                                            LinearLayout.LayoutParams lps = new LinearLayout.LayoutParams(widthPixels, scaledW);
                                            imageView.setLayoutParams(lps);
                                            imageView.setImageDrawable(resource);
                                        }
                                    });
                            xiangqiline.addView(imageView);
                        }
                        for (int imgs = 0; imgs < shangPingModel.getData().getImg().size(); imgs++) {
                            imagePath.add(shangPingModel.getData().getImg().get(imgs).img);
                        }

                        initViews();
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };

    private GetAddressModel getAddressModel;
    public static ArrayList<GetAddressModel.data> list_view = new ArrayList<>();

    private void okHttp_postFromParameters1() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient okHttpClient = new OkHttpClient();
                    RequestBody formBody = new FormBody.Builder().add("uid", Url.USER_ID).add("logintoken", Url.LOGIN_TOKEN)
                            .build();
                    Request request = new Request.Builder().url(Url.GETADDRESSLIST).post(formBody).build();
                    okhttp3.Response response = okHttpClient.newCall(request).execute();
                    String msg = response.body().string();
                    Message message = new Message();
                    message.obj = msg;
                    gethot.sendMessage(message);
                } catch (
                        Exception e) {
                    e.printStackTrace();
                }
            }
        }).
                start();
    }

    int s = 1;
    public Handler gethot = new Handler() {
        public void handleMessage(android.os.Message msg) {
            JSONObject success_Object;
            try {
                if (msg.obj != null) {
                    success_Object = new JSONObject(msg.obj.toString());
                    String code = success_Object.getString("code");
                    if (code.equals("1")) {
                        Gson gson = new Gson();
                        getAddressModel = gson.fromJson(msg.obj.toString(),
                                GetAddressModel.class);
                        list_view.clear();
                        list_view.addAll(getAddressModel.getData());
                        for (int is = 0; is < list_view.size(); is++) {
                            new_list_view.add("" + s);
                        }
                        for (int is1 = 0; is1 < list_view.size(); is1++) {
                            if (list_view.get(is1).getIsdefault().equals("1")) {
                                new_list_view.set(is1, 2 + "");
                            }
                        }
                        myAdapter = new DiZhiAdapter(new_list_view, list_view,
                                ShangPingActivity.this);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };


    @Override
    public void OnBannerClick(int position) {

    }
}
